package com.factorit.business.dao;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.factorit.beans.User;

public class UserDao extends GenericDaoImpl {
	
	public User getUserById(final Long id) {
		User user = (User) getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException {
				Criteria criteria = session.createCriteria(User.class);
				criteria.add(Restrictions.eq("id", id));
				return criteria.uniqueResult();
			}
		});

		return (user);
	}

	public User getUserByUsernameAndPassword(final String username, final String password) {
		User user = (User) getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException {
				Criteria criteria = session.createCriteria(User.class);
				criteria.add(Restrictions.eq("username", username));
				criteria.add(Restrictions.eq("password", password));
				return criteria.uniqueResult();
			}
		});
		User userApp = null;
		if (user != null) {
			userApp = (user);
		}
		return userApp;
	}
}
